﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using YDWin.Base;
using YDServices;
using YDModel;
using DevExpress.Utils;
using YDCommon;

namespace YDWin.Spec
{
    public partial class AddSpecForm : AddForm
    {
        SpecService svr = new SpecService();
        FactoryService fs = new FactoryService();
        SystemParameterService sps = new SystemParameterService();
        public T_Spec editTu = new T_Spec();

        public string state = "";
        public AddSpecForm()
        {
            InitializeComponent();
 
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
          
            T_Spec fact = new T_Spec();
            if (!state.Equals("") && state.Equals("Edit"))
            {
                fact = editTu;
            }

            if (txtSpecName.Text.Trim().Equals(""))
            {
                Utils.ShowPrompt(MessageType.mtWarning, "规格名称不能为空!\n"); 
                return;
            }
            if (cboFactoryName.EditValue==null)
            {
                fact.Factory_ID = 0;
                fact.Factory_Name = "通用";
            }
            else
            {
                fact.Factory_ID = (long)cboFactoryName.EditValue;
                fact.Factory_Name = cboFactoryName.Text;
            }
             
            fact.SpecName = txtSpecName.Text;
            WaitDialogForm sdf = new WaitDialogForm("", "正在保存......");
            if (!state.Equals("") && state.Equals("Edit"))
            {
                svr.EditItem(fact);
            }
            else
            {
                svr.AddItem(fact);
            }
      

            sdf.Close();

            if (svr.HasError)
            {
                Utils.ShowPrompt(MessageType.mtError, "规格信息保存失败：" + svr.Errors);
                svr.Errors.Clear();
                return;
            }

            this.DialogResult = DialogResult.OK;
            this.Close();

        }

        private void AddSpecForm_Load(object sender, EventArgs e)
        {
            //厂家名称下拉框
            var fDs = fs.getComboxList("通用");

            cboFactoryName.Properties.ValueMember = "ID";   //实际要用的字段
            cboFactoryName.Properties.DisplayMember = "FactoryName";    //要显示的字段
            cboFactoryName.Properties.DataSource = fDs;
            cboFactoryName.Properties.SearchMode = DevExpress.XtraEditors.Controls.SearchMode.AutoComplete;//输入之后自动显示并选中下拉内容

    
            if (!state.Equals("") && state.Equals("Edit")) 
            {
                cboFactoryName.Text = editTu.Factory_Name;
                txtSpecName.Text = editTu.SpecName; 
            }
        }
 
    }
}